package com.xie.smfs.entity;

import com.xie.smfs.item.GhostPorcelainItem;
import com.xie.smfs.item.RedGhostCandleItem;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.damage.DamageTypes;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import com.xie.smfs.registry.ModEffects;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.util.math.Vec3d;

/**
 * 抬头鬼实体类 - 特殊行为: 强制玩家抬头并造成持续伤害
 */
public class TaitouGhostEntity extends GhostEntity {
    // 抬头鬼特有属性配置
    private static final boolean HAS_GHOST_DOMAIN = true;
    private static final int GHOST_DOMAIN_LEVEL = 0; // 高级鬼域
    private static final double GHOST_DOMAIN_RADIUS = 32.0;
    private static final char TERROR_LEVEL = 'C'; // A级恐怖


    // 构造函数
    public TaitouGhostEntity(EntityType<TaitouGhostEntity> entityType, World world) {
    super(entityType, world, HAS_GHOST_DOMAIN, GHOST_DOMAIN_LEVEL, GHOST_DOMAIN_RADIUS, TERROR_LEVEL);
    this.ghostLevel = 3; // 设置鬼等级
    this.attackCooldown = 60; // 较长攻击冷却
    initHeadUpAttributes();
}

    // 初始化特有属性
    private int headUpTicks = 0; // 抬头持续时间计时器

    private void initHeadUpAttributes() {
        // 抬头鬼属性: 生命值较低且移动缓慢
        EntityAttributeInstance healthAttribute = this.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH);
        if (healthAttribute != null) healthAttribute.setBaseValue(15.0);
        EntityAttributeInstance speedAttribute = this.getAttributeInstance(EntityAttributes.GENERIC_MOVEMENT_SPEED);
        if (speedAttribute != null) speedAttribute.setBaseValue(0.2); // 降低移动速度
        EntityAttributeInstance attackDamageAttribute = this.getAttributeInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE);
        if (attackDamageAttribute != null) attackDamageAttribute.setBaseValue(6.0);
        EntityAttributeInstance attackKnockbackAttribute = this.getAttributeInstance(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);
        if (attackKnockbackAttribute != null) attackKnockbackAttribute.setBaseValue(0.0);
        EntityAttributeInstance followRangeAttribute = this.getAttributeInstance(EntityAttributes.GENERIC_FOLLOW_RANGE);
        if (followRangeAttribute != null) followRangeAttribute.setBaseValue(32.0);
    }

    // 重写tick方法实现抬头效果
    @Override
    public void tick() {
        super.tick();
        // 攻击冷却递减
        if (attackCooldown > 0) attackCooldown--;
        if (!this.getWorld().isClient) {
            // 随机游荡行为: 每200ticks随机移动一次
            if (isSuppressed() || isDead()) {
                this.setVelocity(Vec3d.ZERO);
                this.getNavigation().stop();
            } else {
                if (this.age % 200 == 0 && getGhostRandom().nextBoolean()) {
                    double radius = 8.0;
                    double x = this.getX() + (getGhostRandom().nextDouble() - 0.5) * radius * 2;
                    double z = this.getZ() + (getGhostRandom().nextDouble() - 0.5) * radius * 2;
                    this.getNavigation().startMovingTo(x, this.getY(), z, 0.8);
                }
            }
            // 随机抬头行为: 每100-300ticks随机抬头一次，持续40ticks
            if (headUpTicks > 0) {
                headUpTicks--;
                this.setPitch(-60.0F); // 保持抬头状态
            } else if (getGhostRandom().nextInt(300) < 5) {
                headUpTicks = 60; // 设置抬头持续时间
                this.setPitch(-60.0F); // 设置抬头角度
            }
        }
    }

    // 抬头鬼特有的杀人规则
    @Override
    protected boolean shouldAttackPlayer(PlayerEntity player) {
        // 只攻击抬头且未手持红蜡烛的玩家
        return !RedGhostCandleItem.isHoldingCandle(player) && player.getPitch() < -30 && this.squaredDistanceTo(player) <= 1024.0 && this.attackCooldown <= 0;
    }

    @Override
    protected void executeAttack(PlayerEntity player) {
        // 抬头鬼攻击逻辑：造成高额伤害并施加失明效果
    // 瞬移到玩家面前
    this.teleport(player.getX(), player.getY(), player.getZ());
    // 获取 mob 伤害类型的注册表项
    RegistryEntry<DamageType> mobDamageType = this.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getEntry(RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("smfs", "ghost"))).orElseThrow(() -> new IllegalStateException("MOB_ATTACK damage type not found"));
    // 创建伤害源
    DamageSource damageSource = new DamageSource(mobDamageType, this);
    player.addStatusEffect(new StatusEffectInstance(StatusEffects.BLINDNESS, 20, 1));

    player.damage(damageSource, 100.0f);
    this.attackCooldown = 20; // 重置攻击冷却
    }
}